package com.passncool.testbed.network;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;

public class SingleFileHTTPServer extends Thread
{
    private byte[] content;

    private byte[] header;

    private int port = 80;

    public SingleFileHTTPServer(String data, String encoding, String MIMEType, int port)
            throws UnsupportedEncodingException
    {
        this(data.getBytes(), encoding, MIMEType, port);
    }

    public SingleFileHTTPServer(byte[] data, String encoding, String MIMEType, int port)
            throws UnsupportedEncodingException
    {
        this.content = data;
        this.port = port;

        String header =
                "HTTP/1.0 200 OK\r\n" + "Server: OneFile 1.0\r\n"
                        + "Content-length: "
                        + this.content.length
                        + "\r\n"
                        + "Content-type: "
                        + MIMEType
                        + "\r\n\r\n";

        this.header = header.getBytes("ASCII");
    }

    public void run()
    {
        try
        {
            ServerSocket sock = new ServerSocket(this.port);
            System.out.println("Accepting connections on port " + sock.getLocalPort());

            Socket connection = null;
            while (true)
            {
                try
                {
                    connection = sock.accept();
                    OutputStream out = new BufferedOutputStream(connection.getOutputStream());
                    InputStream in = new BufferedInputStream(connection.getInputStream());

                    StringBuffer sb = new StringBuffer(80);

                    while (true)
                    {
                        int c = in.read();
                        if (c == '\r' || c == '\n' || c == -1)
                        {
                            break;
                        }
                        sb.append((char) c);
                    }

                    if (sb.toString().indexOf("HTTP/") != -1)
                    {
                        out.write(this.header);
                    }

                    out.write(this.content);
                    out.flush();

                }
                catch (IOException e)
                {

                }
                finally
                {
                    if (connection != null) connection.close();
                }
            }

        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
        }
    }
    
    public static void main(String[] args)
    {
        String mimeType = "text/plain";
        String filePath = "C:/index.html";
        
        try
        {
            FileInputStream fs = new FileInputStream(filePath);
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
            
            int b;
            while((b = fs.read()) != -1)
            {
                byteArray.write(b);
            }
            
            byte[] data = byteArray.toByteArray();
            
            SingleFileHTTPServer http = new SingleFileHTTPServer(data, "ASCII", mimeType, 80);
            http.start();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}
